//
//  RW_AVAudioPlayerTools.m
//  SleepApp
//
//  Created by MAC on 2021/3/23.
//  Copyright © 2021 RiceWang. All rights reserved.
//

#import "RW_AVAudioPlayerTools.h"
#import "NSFileManager+WLExtension.h"
@interface RW_AVAudioPlayerTools ()<AVAudioPlayerDelegate>
@property (nonatomic,strong) NSData *audioData;
@property (nonatomic,strong) NSTimer *timer;
@end
@implementation RW_AVAudioPlayerTools
- (instancetype)init
{
    self = [super init];
    if (self) {
        _volume = 100;
        [self timer];
    }
    return self;
}

#pragma mark -- 初始化
-(void)setAudioPath:(NSString *)audioPath{
    [self setAudioPath:audioPath runLoop:0];
}
/** 设置音频路径 并设置循环次数  */
-(void)setAudioPath:(NSString *)audioPath runLoop:(NSInteger)runLoop{
    NSData *audioData = [NSData dataWithContentsOfFile:audioPath];
    if (!audioData){
        //初始化失败
        if ([self.delegate respondsToSelector:@selector(initAudioError:)]){
            [self.delegate initAudioError:nil];
        }
        return;
    }
    NSError *error = nil;
    self.audioPlayer = [[AVAudioPlayer alloc]initWithData:audioData error:&error];
    if (error){
        NSURL *urlPath = [NSURL URLWithString:audioPath];
        [NSFileManager wl_deleteFilePath:[urlPath path]];
        //初始化失败
        if ([self.delegate respondsToSelector:@selector(initAudioError:)]){
            [self.delegate initAudioError:error];
        }
        return;
    }
    if (self.audioPlayer){
        self.audioPlayer.volume = self.volume/100.f;
        self.audioPlayer.numberOfLoops = runLoop;
        self.audioPlayer.delegate = self;
        [self.audioPlayer prepareToPlay];
        //初始化成功则进行一次歌曲播放状态基本信息回调
        if ([self.delegate respondsToSelector:@selector(initPlayCurrentTime:allCurrentTime:)]){
            [self.delegate initPlayCurrentTime:self.currentTime allCurrentTime:self.duration];
        }
    } else {
        //初始化失败
        if ([self.delegate respondsToSelector:@selector(initAudioError:)]){
            [self.delegate initAudioError:[NSError errorWithDomain:@"未知错误" code:999 userInfo:nil]];
        }
    }
}
#pragma mark -- AVAudioPlayerDelegate
/* 当声音结束时。 如果由于中断而停止播放器，则该方法不被调用。 */
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
    if ([self.delegate respondsToSelector:@selector(playEnd)]){
        [self.delegate playEnd];
    }
}

/* 解码时发生错误 */
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError * __nullable)error{
    if ([self.delegate respondsToSelector:@selector(playError)]){
        [self.delegate playError];
    }
}
#pragma mark -- 属性get方法
-(BOOL)isPlaying{
    return self.audioPlayer.isPlaying;
}
-(NSInteger)duration{
    return self.audioPlayer.duration;
}
-(CGFloat)currentTime{
    return self.audioPlayer.currentTime;
}
#pragma mark -- 属性set方法
-(void)setCurrentTime:(CGFloat)currentTime{
    [self.audioPlayer setCurrentTime:currentTime];
}
-(void)setVolume:(NSInteger)volume{
    _volume = volume;
    if (self.audioPlayer){
        self.audioPlayer.volume = self.volume/100.f;
    }
}
#pragma mark -- 逻辑方法

#pragma mark -- 内部方法
/** 计时器 */
-(void)runTime{
//    NSLog(@"计时器计时器计时器计时器计时器计时器计时器");
    if (self.isPlaying){
        if ([self.delegate respondsToSelector:@selector(currentTime:allCurrentTime:)]){
            [self.delegate currentTime:self.currentTime allCurrentTime:self.duration];
        }
    }
}
/** 停止播放 */
-(void)stopAudio{
    [self.audioPlayer stop];
    if ([self.delegate respondsToSelector:@selector(playStop)]){
        [self.delegate playStop];
    }
}
/** 暂停播放 */
-(void)pauseAudio{
    [self.audioPlayer pause];
    if ([self.delegate respondsToSelector:@selector(playPause)]){
        [self.delegate playPause];
    }
}
/** 开始播放 */
-(void)playAudio{
    [self.audioPlayer play];
    if ([self.delegate respondsToSelector:@selector(playBegin)]){
        [self.delegate playBegin];
    }
}
#pragma mark -- 懒加载
-(NSTimer *)timer{
    if (!_timer){
        _timer = [NSTimer scheduledTimerWithTimeInterval:.5f target:self selector:@selector(runTime) userInfo:nil repeats:YES];
    }
    return _timer;
}
#pragma mark -- 释放当前播放器属性
-(void)releaseVM{
    [_timer invalidate];
    _timer = nil;
    _audioData = nil;
    [_audioPlayer pause];
    _audioPlayer.delegate = self;
    _audioPlayer = nil;
    _delegate = nil;
}
@end
